﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Domain.Repository
{
    /// <summary>
    /// 分页数据存储对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class Page<T> where T : class
    {
        /// <summary>
        /// 默认的页面可装载的数据条目总和
        /// </summary>
        public static readonly long DEFAULT_DATA_SIZE_IN_ONE_PAGE = 50;

        /// <summary>
        /// 页面可装载的数据条目
        /// </summary>
        private long pageSize = DEFAULT_DATA_SIZE_IN_ONE_PAGE;

        /// <summary>
        /// 当前页面的索引
        /// </summary>
        private long pageNumber;

        /// <summary>
        /// 页面数据
        /// </summary>
        private IList<T> rows;

        /// <summary>
        /// 所有的数据条目总数（所有的分页的数据累加）
        /// </summary>
        private long total;


        public Page()
            : this(0, 0, DEFAULT_DATA_SIZE_IN_ONE_PAGE, null)
        {

        }

        /// <summary>
        /// 构建Page分页对象
        /// </summary>
        /// <param name="currentPageNo">当前页面的索引</param>
        /// <param name="totalCount">所有的记录数</param>
        /// <param name="pageSize">每页的记录数</param>
        /// <param name="data">页面数据</param>
        public Page(long currentPageNo, long totalCount, long pageSize, IList<T> data)
        {
            if (0 > pageSize)
                throw new ArgumentException("页面的数据大小不能为0！");
            this.pageSize = pageSize;
            this.pageNumber = currentPageNo;
            this.total = totalCount;
            this.rows = data;
        }

        /// <summary>
        /// 所有页面的数据总数
        /// </summary>
        public long Total
        {
            get
            {
                return this.total;
            }
        }

        /// <summary>
        /// 页面总数
        /// </summary>
        public long TotalPageCount
        {
            get
            {
                if (total % pageSize == 0)
                    return total / pageSize;
                else
                    return total / pageSize + 1;
            }
        }

        /// <summary>
        /// 页面数据数量
        /// </summary>
        public long PageSize
        {
            get
            {
                return pageSize;
            }
        }

        /// <summary>
        /// 页面数据
        /// </summary>
        /// <returns></returns>
        public IList<T> Rows
        {
            get
            {
                return this.rows;
            }
        }

        /// <summary>
        /// 当前页
        /// </summary>
        public long PageNumber
        {
            get
            {
                return pageNumber;
            }
        }

        /// <summary>
        /// 当前页是否有下一页
        /// </summary>
        public bool HasNextPage
        {
            get
            {
                return this.PageNumber < this.TotalPageCount - 1;
            }
        }

        /// <summary>
        /// 当前页是否有前一页
        /// </summary>
        public bool HasPreviousPage
        {
            get
            {
                return this.PageNumber > 1;
            }
        }
    }
}
